{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPT\n",
    "from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes\n",
    "from fastgm import SM4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "299 µs ± 6.61 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "key = b'1' * 16\n",
    "value = b'helloworld' * 1024\n",
    "\n",
    "sm4 = SM4(key)\n",
    "sm4.decrypt_ecb(sm4.encrypt_ecb(value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "170 ms ± 1.22 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "key = b'1' * 16\n",
    "value = b'helloworld' * 1024\n",
    "crypt_sm4 = CryptSM4()\n",
    "\n",
    "crypt_sm4.set_key(key, SM4_ENCRYPT)\n",
    "encrypt_value = crypt_sm4.crypt_ecb(value) #  bytes类型\n",
    "crypt_sm4.set_key(key, SM4_DECRYPT)\n",
    "decrypt_value = crypt_sm4.crypt_ecb(encrypt_value) #  bytes类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def zero_pad(data):\n",
    "    buf_len = len(data)\n",
    "    \n",
    "    if buf_len % 16 == 0:\n",
    "        return data\n",
    "    else:\n",
    "        new_len = (buf_len // 16 + 1) * 16\n",
    "        return data + b'\\0' * (new_len - buf_len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "283 µs ± 6.57 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "key = b'1' * 16\n",
    "value = b'helloworld' * 1024\n",
    "\n",
    "cipher = Cipher(algorithms.SM4(key), mode=modes.ECB())\n",
    "encryptor = cipher.encryptor()\n",
    "enc = encryptor.update(zero_pad(value)) + encryptor.finalize()\n",
    "\n",
    "cipher = Cipher(algorithms.SM4(key), mode=modes.ECB())\n",
    "decryptor = cipher.decryptor()\n",
    "\n",
    "dec = decryptor.update(zero_pad(enc)) + decryptor.finalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gmssl.sm2 import CryptSM2\n",
    "from fastgm import SM2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.12 s ± 22.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "sk = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5'\n",
    "pk = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207'\n",
    "\n",
    "data = b'helloworld' * 1024\n",
    "\n",
    "sm2 = CryptSM2(public_key=pk, private_key=sk)\n",
    "sm2.decrypt(sm2.encrypt(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29.9 ms ± 1.39 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "sk = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5'\n",
    "pk = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207'\n",
    "\n",
    "data = b'helloworld' * 1024\n",
    "\n",
    "sm2 = SM2()\n",
    "\n",
    "enc = sm2.encrypt(pk, data)\n",
    "dec = sm2.decrypt(sk, enc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fastgm import SM3\n",
    "from gmssl.sm3 import sm3_hash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "802 µs ± 21.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "SM3().hash(b'helloworld' * 1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111 ms ± 10.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "sm3_hash(list(b'helloworld' * 1024))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
